package userLoginInterface;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {


        //创建一个集合充当数据源
        ArrayList<User> users = new ArrayList<>();
        User user1 = new User("az", "123123L", "140125200010109854", "12345678912");
        users.add(user1);

        boolean flag = true;
        while (flag) {
            //登录界面
            System.out.println("欢迎来到学生管理系统");
            System.out.println("请选择操作\n1:登录\n2:注册\n3:忘记密码");

            //输入选择
            Scanner sc = new Scanner(System.in);
            int userType = sc.nextInt();

            switch (userType) {
                case 1:
                    login(users);
                    break;
                case 2:
                    register(users);
                    break;
                case 3:
                    forgetPassword(users);
                    break;
                case 4:
                    System.out.println("谢谢使用再见");
                    System.exit(0);
                default:
                    System.out.println("没有这个选项");
                    break;
            }
        }
    }

    //1:登录
    public static void login(ArrayList<User> users) {
        Scanner sc = new Scanner(System.in);

        //键盘录入用户名
        boolean falg1 = true;
        int count = 0;
        boolean idResult = false;
        while (falg1) {
            if (count < 3) {
                System.out.println("请输入用户名");
                String userName = sc.next();
                if (userContain(users, userName)) {
                    falg1 = false;
                    idResult = true;
                } else {
                    System.out.println("用户名未注册请先注册");
                    count++;
                }
            } else {
                falg1 = false;
                return;
            }

        }

        //键盘录入密码
        System.out.println("请输入密码");
        String password = sc.next();


        //键盘录入验证码
        boolean flag2 = true;
        while (flag2) {
            String vc = verificationCode();
            System.out.println("验证码为:" + vc);
            System.out.println("请输入验证码");
            String writeVc = sc.next();
            if (writeVc.equals(vc)) {
                flag2 = false;
                System.out.println("验证码正确");
            } else {
                System.out.println("验证码错误，请重新输入");
            }
        }

        //判断用户名和密码是否正确

        if (idResult && passwordContain(users, password)) {
            System.out.println("登录成功");
        } else {
            System.out.println("密码错误，请重新输入");
            int count1 = 2;
            while (count1 > 0) {
                System.out.println("还可以输入" + count1 + "次");
                String newPassword = sc.next();
                if (password.equals(newPassword)) {
                    System.out.println("登录成功");
                    break;
                } else {
                    if (count1 > 1) {
                        System.out.println("密码错误，请重新输入");
                        count1--;
                    } else if (count1 == 1) {
                        System.out.println("密码错误，请重新登录");
                        count1--;
                    }

                }
            }

        }
    }

    //1.1判断用户名是否注册
    public static boolean userContain(ArrayList<User> users, String userName) {
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            if (user.getUserName().equals(userName)) {
                return true;
            }
        }
        return false;
    }

    //1.2判断密码输入是否正确
    public static boolean passwordContain(ArrayList<User> users, String password) {
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            if (user.getPassword().equals(password)) {
                return true;
            }
        }
        return false;
    }

    //1.3验证码
    public static String verificationCode() {
        //创建一个集合
        ArrayList<Character> list = new ArrayList<>();
        for (int i = 0; i < 26; i++) {
            list.add((char) ('A' + i));
            list.add((char) ('a' + i));
        }

        //随机抽取四个字符
        StringBuilder sb = new StringBuilder();
        Random r = new Random();
        for (int i = 0; i < 4; i++) {
            int index = r.nextInt(list.size());
            char ch = list.get(index);
            sb.append(ch);
        }

        //把一个随机数字添加到末尾
        int number = r.nextInt(10);
        sb.append(number);

        //先变成字符数组再改变顺序
        char[] arr = sb.toString().toCharArray();
        int randomIndex = r.nextInt(arr.length);
        char randomChar = arr[randomIndex];
        arr[randomIndex] = arr[arr.length - 1];
        arr[arr.length - 1] = randomChar;
        return new String(arr);


        
        /*Random r = new Random();
        //创建一个字母数组
        char[] letter = new char[52];
        for (int i = 0; i < letter.length; i++) {
            if (i < 26) {
                letter[i] = (char) ('A' + i);
            } else {
                letter[i] = (char) ('a' + i - 26);
            }
        }

        //先创建密码中的五位字母
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < 5; i++) {
            int randomNum = r.nextInt(52);
            sb.append(letter[randomNum]);
        }

        //将其中的一位随机替换成数字
        int letterIndex = r.nextInt(5) + 1;
        int number = r.nextInt(10);
        String numberStr = String.valueOf(number);
        sb.replace(letterIndex - 1, letterIndex, numberStr);

        //输出验证码
        System.out.println("验证码为:" + sb.toString());

        //返回验证码
        return sb.toString();*/
    }

    //2:注册
    private static void register(ArrayList<User> users) {
        Scanner sc = new Scanner(System.in);
        String userName;
        //输入用户名
        while (true) {
            System.out.println("请输入用户名");
            System.out.println("要求在3~15位之间，包含字母和数字");
            userName = sc.next();
            //检查用户名是否符合规则
            boolean flag = usernameVerify(users, userName);
            if (!flag) {
                System.out.println("用户名不符合规则");
                continue;
            }
            break; // 若用户名有效，跳出循环
        }

        String password;
        //输入密码
        while (true) {
            System.out.println("请输入密码");
            password = sc.next();
            System.out.println("请再次输入密码");
            String rePassWord = sc.next();
            //判断两次输入密码是否一致
            if (passwordVerify(password, rePassWord)) {
                System.out.println("密码正确");
                break; // 若两次密码匹配，跳出循环
            } else {
                System.out.println("两次密码不一致，请重新输入");
            }
        }

        String id;
        //输入身份证号
        while (true) {
            System.out.println("请输入身份证号");
            id = sc.next();
            boolean flag = idVerify(id);
            if (!flag) {
                System.out.println("身份证号错误");
                continue;
            } else {
                System.out.println("身份证号正确");
                break; // 若身份证号有效，跳出循环
            }
        }

        String phone;
        //手机号验证
        while (true) {
            System.out.println("请输入手机号");
            phone = sc.next();
            boolean flag = phoneVerify(phone);
            if (!flag) {
                System.out.println("手机号错误");
                continue;
            } else {
                System.out.println("手机号码格式正确");
                break; // 若手机号有效，跳出循环
            }
        }
        User user = new User(userName, password, id, phone);
        users.add(user);
        System.out.println("注册成功");
    }

    //2.1用户名验证
    public static boolean usernameVerify(ArrayList<User> users, String username) {
        //判断用户名是否重复（通常先验证格式是否正确，再看是否唯一）

        int len = username.length();
        if (len < 3 || len > 15) {
            System.out.println("用户名长度不符合要求，请重新输入");
            return false;
        }


        //判断用户名是否符合字母或数字
        int nummcount = 0;
        int lettercount = 0;
        for (int i1 = 0; i1 < len; i1++) {
            char ch = username.charAt(i1);
            if (!(ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')) {
                return false;
            }
        }

        //判断用户名中是否有字母
        for (int i = 0; i < len; i++) {
            char ch = username.charAt(i);
            if (!(ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')) {
                return false;
            }
        }

        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            if (user.getUserName().equals(username)) {
                System.out.println("用户名重复，请重新输入");
                return false;
            }
        }
        return true;
    }

    //2.2身份证号验证
    public static boolean idVerify(String id) {
        //验证身份证号第一位不为0
        if (id.charAt(0) == '0') {
            return false;
        }

        //验证身份证号为18位
        if (!(id.length() == 18)) {
            return false;
        }

        //验证身份证号前17位为数字,最后一位可为数字，可为X或x
        for (int i = 0; i < id.length() - 1; i++) {
            char ch = id.charAt(i);
            if (ch >= '0' && ch <= '9') {
            } else return false;
        }

        if (id.charAt(id.length() - 1) >= '0' && id.charAt(id.length() - 1) <= '9' || id.charAt(id.length() - 1) == 'X' || id.charAt(id.length() - 1) == 'x') {
        } else return false;

        return true;
    }

    //2.3手机号验证
    public static boolean phoneVerify(String phone) {
        if (phone.charAt(0) == '0') {
            return false;
        }
        if (!(phone.length() == 11)) {
            return false;
        }
        for (int i = 0; i < phone.length(); i++) {
            char ch = phone.charAt(i);
            if (ch >= '0' && ch <= '9') {
            } else {
                return false;
            }
        }
        return true;
    }

    //2.4密码验证
    public static boolean passwordVerify(String password, String rePassWord) {
        if (password.equals(rePassWord)) {
            return true;
        }
        return true;
    }


    //3.忘记密码
    public static void forgetPassword(ArrayList<User> users) {
        //判断用户名是否存在
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入用户名");
        String userName = sc.next();
        if (userContain(users, userName)) {

            //输入身份证号和电话号码
            System.out.println("请输入身份证号码");
            String id = sc.next();
            System.out.println("请输入电话号码");
            String phone = sc.next();

            //判断是否正确
            if (idContain(users, id) && phoneContain(users, phone)) {

                System.out.println("请输入密码");
                String rePassWord = sc.next();
                User user = getEditUser(users, id);
                user.setPassword(rePassWord);
                //users.add(user);
                System.out.println("密码修改成功");
            } else {
                System.out.println("账号信息不匹配，修改失败");
                return;
            }
        } else return;

    }

    //3.1判断身份证号是否一致
    public static boolean idContain(ArrayList<User> users, String id) {
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            if (user.getId().equals(id)) {
                return true;
            }
        }
        return false;
    }

    //3.2判断手机号码是否一致
    public static boolean phoneContain(ArrayList<User> users, String phone) {
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            if (user.getPhone().equals(phone)) {
                return true;
            }
        }
        return false;
    }

    //3.3找到要修改的用户
    public static User getEditUser(ArrayList<User> users, String id) {
        for (int i = 0; i < users.size(); i++) {
            User user = users.get(i);
            if (user.getId().equals(id)) {
                return user;
            }
        }
        return null;
    }
}


